package com.youku.core.view.components
{
   import flash.net.drm.DRMContentData;
   import flash.net.drm.DRMManager;
   import flash.utils.ByteArray;
   import flash.utils.Timer;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.GlobalService;
   import com.youku.GlobalServiceKeys;
   import flash.net.NetConnection;
   import flash.net.NetStream;
   import flash.events.DRMStatusEvent;
   import flash.events.DRMErrorEvent;
   import flash.events.NetStatusEvent;
   import flash.events.TimerEvent;
   import com.youku.core.view.events.BaseVideoEvent;
   import flash.net.drm.LoadVoucherSetting;
   import flash.net.drm.AuthenticationMethod;
   import flash.events.DRMAuthenticationErrorEvent;
   import flash.events.DRMAuthenticationCompleteEvent;
   
   public class DRMStreamVideo extends BaseVideo
   {
      
      public function DRMStreamVideo() {
         super();
         super.NORMAL_BUFFER_TIME = 15;
         this._checkTimer = new Timer(this.CHECK_TIMEOUT,1);
         this._checkTimer.addEventListener(TimerEvent.TIMER,this.onCheckTimeout);
         this._drmManager = DRMManager.getDRMManager();
      }
      
      private var _drmContentData:DRMContentData;
      
      private var _drmManager:DRMManager;
      
      private var _authenticationToken:ByteArray;
      
      private var _checkTimer:Timer;
      
      private const CHECK_TIMEOUT:int = 30000.0;
      
      protected function get playerProxy() : IPlayerProxy {
         return GlobalService.getService(GlobalServiceKeys.PLAYER_PROXY) as IPlayerProxy;
      }
      
      override protected function createItems() : void {
         if(_nc)
         {
            _nc.close();
            _nc = null;
         }
         _nc = new NetConnection();
         _nc.connect(null);
         if(_ns)
         {
            _ns.close();
            _ns = null;
         }
         _ns = new NetStream(_nc);
         _ns.bufferTime = EMPTY_BUFFER_TIME;
         _nsObj = new Object();
         _ns.client = _nsObj;
      }
      
      override protected function addEvents() : void {
         _nsObj.onMetaData = function(param1:Object):void
         {
            onVideoMetaData(param1);
         };
         _ns.addEventListener(DRMStatusEvent.DRM_STATUS,this.onDRMStatusHandler);
         _ns.addEventListener(DRMErrorEvent.DRM_ERROR,this.onDRMErrorHandler);
         _ns.addEventListener(NetStatusEvent.NET_STATUS,this.onNetStatus);
      }
      
      override protected function onNetStatus(param1:NetStatusEvent) : void {
         if(param1.info.code == "NetStream.Play.Start")
         {
            this._checkTimer.reset();
            this._checkTimer.start();
         }
         super.onNetStatus(param1);
      }
      
      private function onCheckTimeout(param1:TimerEvent) : void {
         this._checkTimer.stop();
         var _loc2_:BaseVideoEvent = new BaseVideoEvent(BaseVideoEvent.DRM_BROWSER_NOT_SUPPORT);
         dispatchEvent(_loc2_);
      }
      
      private function onDRMErrorHandler(param1:DRMErrorEvent) : void {
         var _loc2_:BaseVideoEvent = null;
         this._checkTimer.stop();
         if(param1.contentData)
         {
            this._drmContentData = param1.contentData;
            this.tryLocalDRM();
         }
         else
         {
            _loc2_ = new BaseVideoEvent(BaseVideoEvent.DRM_BROWSER_NOT_SUPPORT);
            dispatchEvent(_loc2_);
         }
      }
      
      private function onDRMStatusHandler(param1:DRMStatusEvent) : void {
         this._checkTimer.stop();
         if(param1.contentData)
         {
            this._drmContentData = param1.contentData;
            this.tryLocalDRM();
         }
      }
      
      private function tryLocalDRM() : void {
         this._drmManager.addEventListener(DRMErrorEvent.DRM_ERROR,this.onLocalDRMManagerError);
         this._drmManager.addEventListener(DRMStatusEvent.DRM_STATUS,this.onLocalDRMManagerStatus);
         this._drmManager.loadVoucher(this._drmContentData,LoadVoucherSetting.LOCAL_ONLY);
      }
      
      private function onLocalDRMManagerStatus(param1:DRMStatusEvent) : void {
         this._drmManager.removeEventListener(DRMErrorEvent.DRM_ERROR,this.onLocalDRMManagerError);
         this._drmManager.removeEventListener(DRMStatusEvent.DRM_STATUS,this.onLocalDRMManagerStatus);
         if(param1.voucher != null)
         {
            this.forceDownloadVoucher();
            return;
         }
         if(this._drmContentData.authenticationMethod == AuthenticationMethod.ANONYMOUS)
         {
            this.forceDownloadVoucher();
            return;
         }
         if(this._drmContentData.authenticationMethod == AuthenticationMethod.USERNAME_AND_PASSWORD)
         {
            this.loadAuthenticate();
         }
      }
      
      private function onLocalDRMManagerError(param1:DRMErrorEvent) : void {
         this._drmManager.removeEventListener(DRMErrorEvent.DRM_ERROR,this.onLocalDRMManagerError);
         this._drmManager.removeEventListener(DRMStatusEvent.DRM_STATUS,this.onLocalDRMManagerStatus);
      }
      
      private function loadAuthenticate() : void {
         var _loc1_:String = this._drmContentData.serverURL;
         var _loc2_:String = this._drmContentData.domain;
         var _loc3_:* = "testuser";
         var _loc4_:* = "testpass";
         this._drmManager.addEventListener(DRMAuthenticationErrorEvent.AUTHENTICATION_ERROR,this.onDRMAuthenticationError);
         this._drmManager.addEventListener(DRMAuthenticationCompleteEvent.AUTHENTICATION_COMPLETE,this.onDRMAuthenticationComplete);
         this._drmManager.authenticate(_loc1_,_loc2_,_loc3_,_loc4_);
      }
      
      private function onDRMAuthenticationComplete(param1:DRMAuthenticationCompleteEvent) : void {
         this._authenticationToken = param1.token;
         this._drmManager.setAuthenticationToken(this._drmContentData.serverURL,this._drmContentData.domain,this._authenticationToken);
         this.forceDownloadVoucher();
      }
      
      private function onDRMAuthenticationError(param1:DRMAuthenticationErrorEvent) : void {
      }
      
      private function forceDownloadVoucher() : void {
         this._drmManager.addEventListener(DRMErrorEvent.DRM_ERROR,this.onForceDRMManagerError);
         this._drmManager.addEventListener(DRMStatusEvent.DRM_STATUS,this.onForceDRMManagerStatus);
         this._drmManager.loadVoucher(this._drmContentData,LoadVoucherSetting.ALLOW_SERVER);
      }
      
      private function onForceDRMManagerStatus(param1:DRMStatusEvent) : void {
      }
      
      private function onForceDRMManagerError(param1:DRMErrorEvent) : void {
      }
      
      override protected function playUnloadedPart(param1:Number) : void {
         if(_closed)
         {
            super.playUnloadedPart(param1);
            return;
         }
         playLoadedPart(param1);
      }
   }
}
